<!--{include(file='head.html')}-->
<script type="text/javascript" src="template/jslib/date/WdatePicker.js"></script>
<div id="wrap">
	<!--{include('left.html')}-->
	<div id="maincont">
		<b class="b1"></b><b class="b2"></b><b class="b3"></b><b class="b4"></b>
		<form action="<!--{url('admin','meal','mealsave')}-->" method="post" name="submitform" id="submitform">
		<div id="main_c">
		<!--{if $opertype == "edit"}-->
			<ul class="bk">
				<li>
					<label><em>*</em>套餐名称：</label>
					<p>
						<input class="input_tx" value="<!--{$meal.title}-->" name="title" id="title" type="text" />
					</p>
				</li>
				<li>
					<label><em>*</em>时间：</label>
					<p>
						<input class="input_tx short" value="<!--{date('Y-m-d',$meal.begintime)}-->" name="begintime" id="begintime" type="text" onClick="WdatePicker({dateFmt:'yyyy-MM-dd'})"/>
						-
						<input class="input_tx short" value="<!--{date('Y-m-d',$meal.endtime)}-->" name="endtime" id="endtime" type="text" onClick="WdatePicker({dateFmt:'yyyy-MM-dd'})"/>
					</p>
				</li>
				<li>
					<label><em>*</em>库存：</label>
					<p>
						<input class="input_tx short" value="<!--{$meal.inventory}-->" name="inventory" id="inventory" type="text" />
						(库存必须小于等于组成套餐的单个商品的最小库存，否则会出错)
					</p>
				</li>
				<li>
					<label>排序：</label>
					<p>
						<input class="input_tx short" value="<!--{$meal.order}-->" name="order" type="text" />
					</p>
				</li>
				<li>
					<label><em>*</em>价格：</label>
					<p>
						原价 <input type="text" class="input_tx mini" value="<!--{getPrice($meal.oldprice)}-->" name="oldprice" id="oldprice" readonly />
						现价 <input type="text" class="input_tx mini" value="<!--{getPrice($meal.price)}-->" name="price" id="price"  />
					</p>
				</li>
			</ul>
			
			<ul class="bk">
				<li>
					<label>套餐描述：</label>
					<p>
						<textarea name="desc" id="desc" class="text_tx"><!--{$meal.desc}--></textarea>
					</p>
				</li>
			</ul>
			
			<table class="tableStyle" border="0" cellspacing="0" cellpadding="0">
				<thead>
					<tr align="center">
						<th width="10%">操作</th>
						<th width="30%">搭配商品</th>
						<th width="50%">名称</th>
						<th width="10%">原价(￥)</th>
					</tr>
				</thead>
				<tbody id="seltbody">
					<!--{foreach $mealitems item}-->
						<tr align="center" id="tr_<!--{$item.itemid}-->">
							<td>
								<a href='javascript:void(0);'  onclick='$.oper.up(this);return false;' class='up'></a>
								<a href='javascript:void(0);'  onclick='$.oper.down(this);return false;' class='down'></a>
								<a href='javascript:void(0);'  onclick='ItemSelect.del(<!--{$item.itemid}-->);return false;' class='del'></a>
								<input type="text" hidden="" value="key_<!--{$item.itemid}-->" name="itemid[]"/>
							</td>
							<td class="forimg">
								<img src="<!--{$item.itemimg}-->_50x50.jpg" width="50" height="50" onerror="this.onerror='',this.src='images/nopicture.jpg'"/>
							</td>
							<td class='forname'>
								<!--{$item.itemname}-->
							</td>
							<td class="forprice"><!--{getPrice($item.price)}--></td>
						</tr>
					<!--{/foreach}-->
				</tbody>
			</table>
			
			<ul class="bk">
				<li>
					<label></label>
					<p>
						<a href="javascript:void(0);" onclick="ItemSelect.toggle(this);return false;" class="bt_style2">选择商品</a>
					</p>
				</li>
			</ul>
			
			<ul class="bk none" id="outerframe">
				<li>
					<iframe src="about:blank" name="myiframe" id="myiframe" border="0" frameborder="0" width="100%" height="400" onload="this.height=myiframe.document.body.scrollHeight"></iframe>
				</li>
			</ul>
			
		<!--{elseif $opertype=="add"}-->
			<ul class="bk">
				<li>
					<label><em>*</em>套餐名称：</label>
					<p>
						<input class="input_tx" value="" name="title" id="title" type="text" />
					</p>
				</li>
				<li>
					<label><em>*</em>时间：</label>
					<p>
						<input class="input_tx short" value="" name="begintime" id="begintime" type="text" onClick="WdatePicker({dateFmt:'yyyy-MM-dd'})"/>
						-
						<input class="input_tx short" value="" name="endtime" id="endtime" type="text" onClick="WdatePicker({dateFmt:'yyyy-MM-dd'})"/>
					</p>
				</li>
				<li>
					<label><em>*</em>库存：</label>
					<p>
						<input class="input_tx short" value="" name="inventory" id="inventory" type="text" />
						(库存必须小于等于组成套餐的单个商品的最小库存，否则会出错)
					</p>
				</li>
				<li>
					<label>排序：</label>
					<p>
						<input class="input_tx short" value="" name="order" type="text" />
					</p>
				</li>
				
				<li>
					<label><em>*</em>价格：</label>
					<p>
						原价 <input type="text" class="input_tx mini" name="oldprice" id="oldprice" readonly />
						现价 <input type="text" class="input_tx mini" name="price" id="price"  />
					</p>
				</li>
			</ul>
			
			<ul class="bk">
				<li>
					<label>套餐描述：</label>
					<p>
						<textarea name="desc" id="desc" class="text_tx"></textarea>
					</p>
				</li>
			</ul>
			
			<table class="tableStyle" border="0" cellspacing="0" cellpadding="0">
				<thead>
					<tr align="center">
						<th width="10%">操作</th>
						<th width="30%">搭配商品</th>
						<th width="50%">名称</th>
						<th width="10%">原价(￥)</th>
					</tr>
				</thead>
				<tbody id="seltbody">
				</tbody>
			</table>
			
			<ul class="bk">
				<li>
					<label></label>
					<p>
						<a href="javascript:void(0);" onclick="ItemSelect.toggle(this);return false;" class="bt_style2">展开</a>
					</p>
				</li>
			</ul>
			
			<ul class="bk none" id="outerframe">
				<li>
					<iframe src="about:blank" name="myiframe" id="myiframe" border="0" frameborder="0" width="100%" height="400" onload="this.height=myiframe.document.body.scrollHeight"></iframe>
				</li>
			</ul>
		<!--{/if}-->

			<div class="bt_bk2">
				<input type="hidden" value="<!--{$opertype}-->" name="opertype" />
				<input type="hidden" value="<!--{def($mealid)}-->" name="mealid"/>
				<a href="javascript:void(0);" class="btn" onclick="jssubmit('meal');return false;"><span>保存</span></a>
			</div>
		</div>
		</form>
		<b class="b4b"></b><b class="b3b"></b><b class="b2b"></b><b class="b1b"></b>
	</div>
</div>

<table class="none">
	<tbody id="foritem">
		<tr align="center" id="tr_{itemid}">
			<td>
				<a href='javascript:void(0);'  onclick='$.oper.up(this);return false;' class='up'></a>
				<a href='javascript:void(0);'  onclick='$.oper.down(this);return false;' class='down'></a>
				<a href='javascript:void(0);'  onclick='ItemSelect.del({itemid});return false;' class='del'></a>
				<input type="text" hidden="" value="{itemid}" name="itemid[]"/>
			</td>
			<td class="forimg"></td>
			<td class='forname'></td>
			<td class="forprice"></td>
		</tr>
	</tbody>
</table>
<!--{include(file='foot.html')}-->
<script type="text/javascript">
var ItemSelect  = {
	tbody	 :$("#seltbody"),
	foritem	 :$("#foritem"),
	itemcount:parseInt("<!--{if $mealid}--><!--{count($mealitems)}--><!--{else}-->0<!--{/if}-->"),
	display  :false,
	toggle   :function(obj) {
		if(this.display) {
			$.oper.hideiframe();
			$(obj).text("选择商品");
		} else {
			$.oper.showiframe("<!--{url('admin','select','selectitem','type=iframe')}-->");
			$(obj).text("收起");
		}
		this.display = !this.display;
	},
	add:function(itemid) {
		if(this.itemcount >= 5) {
			jAlert("您至多选择5个商品做一个搭配销售","警告");
			return false;
		}
		var $frame	= $(window.frames["myiframe"].document),
			$html = this.foritem.clone(),
			name  = $.trim($("#item_"+itemid,$frame).text()),
			price = parseFloat($("#price_"+itemid,$frame).text()).toFixed(2),
			img	  = $("#img_"+itemid,$frame).html();
		$html.find(".forimg").html(img);
		$html.find(".forname").html(name);
		$html.find(".forprice").text(price);
		$html.find(".itemid").val(itemid);
		this.tbody.append($html.html().replace(/{itemid}/g,itemid));
		$("#oper_"+itemid,$frame).html("<font color='red'>已添加</font>");
		this.itemcount ++;
		this.calprice();
	},
	calprice:function() {
		var price  = 0;
		$(".forprice",this.tbody).each(function(){
			price += parseFloat($(this).text()) || 0;
		});
		$("#oldprice").val(price.toFixed(2));
	},
	del:function(itemid) {
		var $frame = $(window.frames["myiframe"].document);
		$("#tr_"+itemid).remove();
		$("#oper_"+itemid,$frame).html("<a href='javascript:void(0);' onclick='window.parent.ItemSelect.add("+itemid+");return false;' class='select'></a>");
		this.itemcount --;
		this.calprice();
	}
};
</script>
